package leetcode;

/**
 * @author yinlei
 * @date 2018/6/20
 * @since 1.0.0
 */
public class N009PalindromeNumber {

    public static void main(String[] args) {

        System.out.println(Integer.MAX_VALUE);
        System.out.println(isPalindrome(Integer.MAX_VALUE));
        System.out.println(isPalindrome(-121));
        System.out.println(isPalindrome(121));
        System.out.println(isPalindrome(0));
        System.out.println(isPalindrome(1));
        System.out.println(isPalindrome(1221));
        System.out.println(isPalindrome(12321));
        System.out.println(isPalindrome(88888));
        System.out.println(isPalindrome(12341));
        System.out.println(isPalindrome(10));
        System.out.println(isPalindrome(11));
        System.out.println(isPalindrome(1000021));
    }

    // 边计算／边对比
    static public boolean isPalindrome(int x) {

        if (x < 0) return false;
        if (x < 10)  return true;

        int maxFactor = 1;
        int d = x;

        // 获取高位系数
        while (d / maxFactor > 9) {
            maxFactor *= 10;
        }

        while (d > 0) {
            int left = d / maxFactor;
            int right = d % 10;

            if (left != right) {
                return false;
            }

            d = (d % maxFactor) / 10;
            maxFactor /= 100;
        }

        return true;
    }

    // 转换数组，然后对比结果
//    static public boolean isPalindrome(int x) {
//
//        if (x < 0) return false;
//        if (x < 10)  return true;
//
//        int[] array = new int[10];
//        int index = 0;
//
//        int d = x;
//
//        while (d > 0) {
//            array[index++] = d % 10;
//            d = d / 10;
//        }
//
//        // 奇偶长度，中间点不同
//        int mid = index % 2 == 0 ? index : (index - 1) / 2;
//        for (int i = 0; i < mid; i++) {
//            if (array[i] != array[index - i - 1])
//                return false;
//        }
//
//        return true;
//    }
}
